﻿function Role() {
    var self = this;
    self.ShowWindow = function (dataList) {

        var ids = new Array();
        var list = $("#dataList").DataTable().rows({ selected: true }).data();
        $.each(list, function (index, item) {
            ids.push(item.id);
        });

        if (ids.length === 0) {
            window.app.message("请选中一项。");
            return;
        }
        if (ids.length > 1) {
            window.app.message("只能选中一项。");
            return;
        }

        parent.$('#ajaxModal').remove();

        var $remote = "/Permission/Index/?owner=role&ownerId=" + ids[0]
            , $modal = $('<div id="ajaxModal">用来做容器</div>');
        parent.$('body').append($modal);

        $modal.load($remote, function () {
            var newmodal = $modal.children("div").modal();
            newmodal.modal('show');
        });
    }
    self.OrgDataWindow = function (dataList) {

        var ids = new Array();
        var list = $("#dataList").DataTable().rows({ selected: true }).data();
        $.each(list, function (index, item) {
            ids.push(item.id);
        });

        if (ids.length === 0) {
            window.app.message("请选中一项。");
            return;
        }
        if (ids.length > 1) {
            window.app.message("只能选中一项。");
            return;
        }

        parent.$('#ajaxModal').remove();

        var $remote = "/Permission/Org/?roleId=" + ids[0]
            , $modal = $('<div id="ajaxModal">用来做容器</div>');
        parent.$('body').append($modal);

        $modal.load($remote, function () {
            var newmodal = $modal.children("div").modal();
            newmodal.modal('show');
        });
    }
    self.relation = function () {

        var $dataList = $(event.currentTarget).parents('.modal-dialog').find(".dataTable");
        var checkboes = $dataList.find(".checkColumn input:checked");

        var output = {
            userIds: ['@ViewBag.userId'],
            roleIds: []
        }

        checkboes.each(function (index, item) {
            output.roleIds.push(item.value);
        });
        $.post($("#roleWindowDataForm").data("url"), output, function (result) {
            if (data.errors.length > 0) {
                swal("出错啦！", data.errors, "error");

            } else {
                window.app.message("保存成功。");
            }
        });


    }
}

